package book.controller;

import book.dao.fajinDao;
import book.entity.*;
import book.service.*;
import com.alibaba.fastjson.JSON;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("guihuan")
public class GuiHuanController {

    @Autowired
    guihuanService guihuan;

    @Autowired
    TushuServices tushu;

    @Autowired
    fajinDao fajin;

    @Autowired
    DuzheServices duzhe;

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        return "guihuan/guihuan";
    }

    @RequestMapping("/tuihui")
    public String tuihui(HttpServletRequest request){
        return "chushou/tuihui";
    }


    @RequestMapping(value = "all",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> cha(@RequestParam("offset") int offset,@RequestParam("limit") int limit,jilu condition){
        List<jilu> list=guihuan.getAll(offset,limit,condition);
        Map<String, Object> map = new HashMap<String,Object>();
        int total = guihuan.getCount();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @RequestMapping(value = "getById",method = RequestMethod.GET)
    public String getById(HttpServletRequest request,@RequestParam("id") String id){
        jilu j = guihuan.getById(id);
        List<fajin> list=fajin.getAll();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String shijian = formatter.format(j.getJy_jiechu());
        request.setAttribute("jilu",j);
        request.setAttribute("shijian",shijian);
        request.setAttribute("fajin",list);
        return "guihuan/add";
    }

    @RequestMapping(value = "tushuup",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> gaitushu(tushu condition){
        System.err.println(JSON.toJSONString(condition));
        Map<String, Object> map = new HashMap<String,Object>();
        tushu.updatushu(condition);
        map.put("result",true);
        return map;
    }

    @RequestMapping(value = "duzheup",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> duzhe(duzhe condition){
        System.err.println(JSON.toJSONString(condition));
        Map<String, Object> map = new HashMap<String,Object>();
        duzhe.updaduzhe(condition);
        map.put("result",true);
        return map;
    }
}